from PySide6 import QtWidgets, QtCore, QtGui
import pynput
import time


def map_mouse_position(position):
    # 获取所有屏幕
    screens = QtWidgets.QApplication.screens()
    print(screens)
    for screen in screens:
        # 获取屏幕的geometry
        screen_geometry = screen.geometry()
        # 判断鼠标是否在当前屏幕上
        if screen_geometry.contains(position):
            # 如果在，则直接返回屏幕上的位置
            return screen_geometry.topLeft() + position - screen.availableGeometry().topLeft()
    # 如果鼠标不在任何屏幕上，则返回主屏幕的位置
    return screens[0].mapFromGlobal(position)


def on_mouse_move(x, y):
    # 将全局坐标转换为逻辑坐标
    position = QtCore.QPoint(x, y)
    mapped_position = map_mouse_position(position)
    print(f"Mouse moved to logical position: {mapped_position}")


def main():
    # 创建一个鼠标监听器
    listener = pynput.mouse.Listener(on_move=on_mouse_move)
    listener.start()
    # 保持程序运行
    while True:
        time.sleep(1)


if __name__ == "__main__":
    main()